package controller.business.fundFinanceSelect;

import base.response.OffsetLimitPage;
import controller.common.BaseController;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import services.business.BusinessInfoServices;
import services.business.InspectorFundServices;
import services.business.InspectorInfoServices;
import services.dictionary.FundSetServices;
import services.specific.FinanceSelectServices;
import util.DateUtil;
import util.io.PoiExcelCreateUtil;
import wt.entity.specific.FinanceSelect;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("business/fundFinanceSelect")
public class FundFinanceSelectController extends BaseController {


    @Autowired
    BusinessInfoServices businessInfoServices;
    @Autowired
    InspectorInfoServices inspectorInfoServices;
    @Autowired
    FundSetServices fundSetServices;
    @Autowired
    FinanceSelectServices financeSelectServices;

    @Autowired
    InspectorFundServices inspectorFundServices;


    @RequiresPermissions("business:fundFinanceSelect:default")
    @RequestMapping("index")
    public String index() {
        return "business/fundFinanceSelect/index";
    }


    @RequiresPermissions("business:fundFinanceSelect:default")
    @ResponseBody
    @RequestMapping("select")
    public OffsetLimitPage select(
            String expertName,
            String idCard,
            String bankName,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            Integer offset,
            Integer limit
    ) throws Exception {
        if (endTime != null) {
            endTime = DateUtil.moveDate(endTime, 1);
        }
        return financeSelectServices.select(expertName, idCard, bankName, startTime, endTime, offset, limit);
    }

    @RequiresPermissions("business:fundFinanceSelect:default")
    @RequestMapping(value = "exportExcel")
    public void exportExcel(
            String expertName,
            String idCard,
            String bankName,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime,
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime,
            HttpServletResponse response) throws Exception {
        if (endTime != null) {
            endTime = DateUtil.moveDate(endTime, 1);
        }

    List<FinanceSelect>  financeSelectList=   financeSelectServices.select(expertName, idCard, bankName, startTime, endTime);
        List<Map> mapList = new ArrayList<Map>(financeSelectList.size());
        for (FinanceSelect financeSelect : financeSelectList) {
            Map map = new HashMap();
            map.put("idType", "居民身份证");
            map.put("taxType", "自行负担");
            map.put("name", financeSelect.getName());
            map.put("idCard", financeSelect.getIdCard());
            map.put("phone", financeSelect.getPhone());
            map.put("bankName", financeSelect.getBankName());
            map.put("bankCard", financeSelect.getBankCard());



            map.put("money", financeSelect.getTotalMoney().doubleValue());
            map.put("tax", getTax(financeSelect.getTotalMoney()).doubleValue());
            map.put("resultMoney", financeSelect.getTotalMoney().subtract(new BigDecimal((Double) map.get("tax"))).doubleValue());
            mapList.add(map);
        }
        PoiExcelCreateUtil poi = new PoiExcelCreateUtil(8);
        CellStyle headStyle = poi.createCellStyle(PoiExcelCreateUtil.FONT_HEIGHT_DEFAULT, PoiExcelCreateUtil.FONT_WEIGHT_BOLE, PoiExcelCreateUtil.FONT_NAME_SONG, null);
        CellStyle bodyStyle = poi.createCellStyle(PoiExcelCreateUtil.FONT_HEIGHT_DEFAULT, PoiExcelCreateUtil.FONT_WEIGHT_NORMAL, PoiExcelCreateUtil.FONT_NAME_SONG, null);
        CellStyle moneyStyle = poi.createCellStyle(PoiExcelCreateUtil.FONT_HEIGHT_DEFAULT, PoiExcelCreateUtil.FONT_WEIGHT_NORMAL, PoiExcelCreateUtil.FONT_NAME_SONG, "0.00");


        poi.createSheet("sheet1",
                null,
                new String[]{"姓名", "身份证号", "手机号码", "银行卡号", "开户行", "应发(元)", "税(元)", "实发(元)"},
                new String[]{"name", "idCard", "phone", "bankCard", "bankName", "money", "tax", "resultMoney"},
                new int[]{4000, 8000, 6000, 8000, 4000, 4000, 4000, 4000},
                null, headStyle,
                new CellStyle[]{bodyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle, moneyStyle, moneyStyle, moneyStyle},
                mapList
        );

        poi.setColumnSize(14);
        poi.createSheet("sheet2",
                null,
                new String[]{"*证照类型", "*证照号码", "姓名", "手机号码", "税款负担方式", "*收入额", "基本养老保险费", "基本医疗保险费", "失业保险费", "住房公积金", "允许扣除的税费", "其他", "准予扣除的捐赠额", "减免税额"},
                new String[]{"idType", "idCard", "name", "phone", "taxType", "money", "null", "null", "null", "null", "tax", "null", "null", "null"},
                new int[]{8000, 8000, 4000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000, 6000},
                null, headStyle,
                new CellStyle[]{bodyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle, moneyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle, bodyStyle},
                mapList
        );
        poi.write2Response(response, "统计表_" + new Date().getTime());
    }


    public BigDecimal getTax(BigDecimal count) {
        if (count.doubleValue() <= 800) {
            return new BigDecimal(0D);
        } else if (count.doubleValue() <= 4000) {
            return (count.subtract(BigDecimal.valueOf(800.00D)).multiply(BigDecimal.valueOf(0.20D)));
        } else {
            return count.multiply( BigDecimal.valueOf(0.16D));
        }
    }


}
